package com.shoujifeng.snowmusic.player;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.utils.MusicPlayer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MusicPlayActivity extends Activity implements SensorEventListener {
	private Button 		previousBtn; 	
	private Button 		repeatBtn; 		
	private Button 		playBtn; 		
	private Button     shuffleBtn; 		
	private Button     nextBtn; 		
	private TextView   titleTextView;
	private TextView 	artistTextView;
	private SensorManager 	sensorManager;
	
	private float lastX, lastY, lastZ;

	private static final int SPEED_SHRESHOLD = 2000;

	private static final int UPTATE_INTERVAL_TIME = 70;
	
	private long lastUpdateTime;
	public static LrcView lrc_view ;
	public Intent intent;
	public MusicPlayer musicPlayer;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music_play);
		musicPlayer = new MusicPlayer(MusicPlayActivity.this);
		init();
		musicPlayer.startPlay();
		setTextView();
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
	}

	private void init() {
		previousBtn =    (Button) findViewById(R.id.previous_music);
		repeatBtn   =    (Button) findViewById(R.id.repeat_music);
		playBtn     =    (Button) findViewById(R.id.play_music);
		shuffleBtn  =    (Button) findViewById(R.id.shuffle_music);
		nextBtn     =    (Button) findViewById(R.id.next_music);
		titleTextView =  (TextView) findViewById(R.id.title);
		artistTextView = (TextView) findViewById(R.id.artist);
		
		ViewOnclickListener viewOnclickListener = new ViewOnclickListener();
		previousBtn.setOnClickListener(viewOnclickListener);
		repeatBtn.setOnClickListener(viewOnclickListener);
		playBtn.setOnClickListener(viewOnclickListener);
		shuffleBtn.setOnClickListener(viewOnclickListener);
		nextBtn.setOnClickListener(viewOnclickListener);
		
		//intent = new Intent();
		lrc_view = (LrcView) findViewById(R.id.lrcShowView);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_GAME);

	}

	@Override
	protected void onStop() {

		sensorManager.unregisterListener(this);
		super.onStop();
	}


	private void setTextView() {
		titleTextView.setText(MusicPlayer.musicInfos.get(MusicPlayer.position)
				.getTitle());
		artistTextView.setText(MusicPlayer.musicInfos.get(MusicPlayer.position)
				.getArtist());
	}


	private class ViewOnclickListener implements OnClickListener {
		

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.play_music:
				musicPlayer.play();
				break;
			case R.id.previous_music: 
				musicPlayer.previous_music();
				
				break;
			case R.id.next_music: 
				musicPlayer.next_music();

				break;
			case R.id.repeat_music: 
				musicPlayer.repeat_music();
				break;
			case R.id.shuffle_music: 
				musicPlayer.shuffle_music();
				break;
			}
		}
	}


	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub

		long currentUpdateTime = System.currentTimeMillis();

		long timeInterval = currentUpdateTime - lastUpdateTime;

		if (timeInterval < UPTATE_INTERVAL_TIME)
			return;

		lastUpdateTime = currentUpdateTime;

		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];


		float deltaX = x - lastX;
		float deltaY = y - lastY;
		float deltaZ = z - lastZ;

		lastX = x;
		lastY = y;
		lastZ = z;

		double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
					* deltaZ)
					/ timeInterval * 10000;

			if (speed >= SPEED_SHRESHOLD) {
				System.out.println("��һ��");
				musicPlayer.next_music();
			}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}
	
	

}
